package com.mike.web;

import org.springframework.util.ReflectionUtils;
import org.springframework.web.WebApplicationInitializer;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
 * @Description 调用MyWebApplicationInitializer的onstartup方法
 * @Date 2021/8/4 14:40
 * @Created by mike
 */
public class MyServletContainerInitializer implements ServletContainerInitializer {
    @Override
    public void onStartup(Set<Class<?>> set, ServletContext servletContext) {
        List<WebApplicationInitializer> initializerList = new LinkedList<>();
        if(set != null){
            for (final Class<?> aClass : set) {
                if(!aClass.isInterface() && !Modifier.isAbstract(aClass.getModifiers()) && WebApplicationInitializer.class.isAssignableFrom(aClass)){
                    try {
                        // 反射创建WebApplicationInitializer
                        initializerList.add((WebApplicationInitializer) ReflectionUtils.accessibleConstructor(aClass).newInstance());
                    } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | InstantiationException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        if(initializerList.isEmpty()){}
    }
}
